package mybaidu

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"public/myhttp"
	"strings"
)

/*
  人脸检测
  image: 图片base64 现支持PNG、JPG、JPEG、BMP，不支持GIF图片
  face_field: 包括age,beauty,expression,face_shape,gender,glasses,landmark,race,quality,face_type信息
逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度 例如：faceshape,facetype
*/
func FaceDetect(imageBase64, face_field string) (result FaceDetectRet) {
	var req ReqFaceDetect
	req.Image = imageBase64
	req.Image_type = "BASE64"
	req.Max_face_num = 1
	req.Face_field = face_field
	req.Face_type = "LIVE" //生活照

	myhttp.SendPost(req, &result, FACEDETECTURL+OnGetAccessToken())
	return
}

/*
  人脸库添加人脸
*/
func FaceAdd(imageBase64, group_id, user_id string) (result FaceAddRet) {
	var req ReqFaceAdd
	req.Image = imageBase64
	req.Image_type = "BASE64"
	req.Group_id = group_id
	req.User_id = user_id
	req.Quality_control = "NONE"
	req.Liveness_control = "NONE"

	myhttp.SendPost(req, &result, FACEADDURL+OnGetAccessToken())
	return
}

/*
	人脸库搜索人脸
	group_id_list:必须 从指定的group中进行查找 用逗号分隔，上限10个
	user_id: 不是必须  当需要对特定用户进行比对时，指定user_id进行比对。即人脸认证功能。
	max_user_num:不是必须 查找后返回的用户数量。返回相似度最高的几个用户，默认为1，最多返回20个。
*/
func FaceSearch(imageBase64, group_id_list, user_id string, max_user_num int) (result FaceSearchRet) {
	var req interface{}
	if len(user_id) > 0 {
		var param ReqFaceSearch2
		param.Image = imageBase64
		param.Image_type = "BASE64"
		param.Group_id_list = group_id_list
		param.User_id = user_id
		param.Quality_control = "NONE"
		param.Liveness_control = "NONE"
		if max_user_num == 0 { //默认为1
			max_user_num = 1
		}
		param.Max_user_num = max_user_num
		req = param
	} else {
		var param ReqFaceSearch
		param.Image = imageBase64
		param.Image_type = "BASE64"
		param.Group_id_list = group_id_list
		param.Quality_control = "NONE"
		param.Liveness_control = "NONE"
		if max_user_num == 0 { //默认为1
			max_user_num = 1
		}
		param.Max_user_num = max_user_num
		req = param
	}
	myhttp.SendPost(req, &result, FACESEARCHURL+OnGetAccessToken())
	return
}

/*
  获取用户人脸列表
*/
func FaceGetList(group_id, user_id string) (result FaceGetListRet) {
	var req ReqFaceGetList
	req.Group_id = group_id
	req.User_id = user_id

	myhttp.SendPost(req, &result, FACEADDURL+OnGetAccessToken())
	return
}

/*
  获取用户组中的用户列表
  group_id	是	string	用户组id，长度限制48B
  start	否	uint32	默认值0，起始序号
  length	否	uint32	返回数量，默认值100，最大值1000

*/
func GroupGetUsers(group_id string, start, length int) (result GroupGetUsersRet) {
	if length == 0 {
		length = 100
	}
	var req ReqGroupGetUsers
	req.Group_id = group_id
	req.Start = start
	req.Length = length

	myhttp.SendPost(req, &result, GROUPGETUSERSURL+OnGetAccessToken())
	return
}

/*
  获取组列表
  start	否	uint32	默认值0，起始序号
  length	否	uint32	返回数量，默认值100，最大值1000

*/
func GroupGetList(start, length int) (result GroupGetListRet) {
	if length == 0 {
		length = 100
	}
	var req ReqGroupGetUsers
	req.Start = start
	req.Length = length

	myhttp.SendPost(req, &result, GROUPGETLISTURL+OnGetAccessToken())
	return
}

/*
  人脸比对
*/
func FaceMatch(imageBase64 []string) (result FaceMatchRet) {
	var req []ReqFaceMatch
	for _, v := range imageBase64 {
		var tmp ReqFaceMatch
		tmp.Image = v
		tmp.Image_type = "BASE64"
		tmp.Face_type = "LIVE"
		tmp.Quality_control = "NONE"
		tmp.Liveness_control = "NONE"
		req = append(req, tmp)
	}
	myhttp.SendPost(req, &result, FACEMATCHURL+OnGetAccessToken())
	return
}

//获取身份证识别信息
func GetIdCardInfo(imageBase64 string) (result BaiduResInfo) {

	www := IDCARDURL + OnGetAccessToken()
	// ff, _ := ioutil.ReadFile(dir) //我还是喜欢用这个快速读文件
	// img_base64 := base64.StdEncoding.EncodeToString(ff)

	postValues := url.Values{}
	postValues.Set("detect_direction", "true")
	postValues.Set("id_card_side", "front")
	postValues.Set("image", imageBase64)

	httpClient := http.Client{}
	httpReq, _ := http.NewRequest("POST", www, strings.NewReader(postValues.Encode()))
	httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	httpResp, _ := httpClient.Do(httpReq)
	body, _ := ioutil.ReadAll(httpResp.Body)
	defer httpResp.Body.Close()
	fmt.Println(string(body))

	json.Unmarshal(body, &result)
	return
}
